/*
* pps_bom
*/
package com.pitop.mould.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MouldException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.mould.entity.Mould;
import com.pitop.mould.service.MouldSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模具 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/mould")
@Slf4j
@Api(value = "模具控制器", description = "模具控制器")
public class MouldCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private MouldSV mouldSV;

    /**
     * 查询Mould一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Mould load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Mould mould = mouldSV.load(id, code);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 查询Mould一个详情信息，(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public Mould get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Mould mould = mouldSV.get(id, code);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 根据条件id查询Mould一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Mould loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Mould mould = mouldSV.loadById(id);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 根据条件code查询Mould一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Mould loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Mould mould = mouldSV.loadByCode(code);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 根据条件id查询Mould一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public Mould getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Mould mould = mouldSV.getById(id);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 根据条件code查询Mould一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public Mould getByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Mould mould = mouldSV.getByCode(code);
        log.info(JSON.toJSONString(mould));
        return mould;
    }

    /**
     * 查询Mould信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Mould信息集合", notes = "查询Mould信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称",  paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Mould> list(@RequestBody @ApiIgnore Mould mould, Integer curPage, Integer pageSize) {
        Page<Mould> page = new Page<Mould>(pageSize, curPage);
        List<Mould> moulds = mouldSV.list(mould, page.genRowStart(), page.getPageSize());
        int total = mouldSV.count(mould);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return moulds;
    }

    /**
     * 查询Mould信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Mould信息集合", notes = "查询Mould信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<Mould> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<Mould> page = new Page<Mould>(pageSize, curPage);
        List<Mould> moulds = mouldSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = mouldSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return moulds;
    }

    /**
     * 统计Mould信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Mould信息数量根据主键", notes = " 统计Mould信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code) {
        return mouldSV.count(id, code);
    }

    /**
     * 统计Mould信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Mould信息数量", notes = "统计Mould信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[开发Develop，稳定Stable，停用 Stop]", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Mould mould) {
        return mouldSV.count(mould);
    }

    /**
     * 创建Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Mould", notes = "创建Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public Mould build(@RequestBody @ApiIgnore Mould mould) {
        mouldSV.save(mould);
        return mould;
    }

    /**
     * 修改Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Mould", notes = "修改Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[开发Develop，稳定Stable，停用 Stop]", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public Mould modify(@RequestBody @ApiIgnore Mould mould) {
        if(mould == null) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if(StringTools.isEmpty(mould.getCode())) {
            throw new MouldException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldSV.modify(mould);
        return mould;
    }

    /**
     * 删除Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Mould", notes = "删除Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        mouldSV.delete(id, code);
        return BeanRet.create(true, "删除Mould成功");
    }

}
